package mydictionary;

import javax.swing.text.html.HTMLDocument;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @author Administrator
 * 工具的具体实现类
 */
public class DictionaryDaoMap implements DictionaryDao {
    //定义集合对象：English作为key，实体类对象作为value
     private HashMap<String,MyDctionary> map=new HashMap<String,MyDctionary>();
    @Override
    public void save(MyDctionary myDctionary) {
        //将传入的参数myDictionary保存在集合中
        String key=myDctionary.getEnglish();
        map.put(key,myDctionary);
    }

    @Override
    public MyDctionary query(String name) {
        //根据传入的参数name（英文）,得到词典对象
        //判断Map中是否包含key，若存在，返回value
        if(map.containsKey(name)){
            return map.get(name);
        }
        return null;
    }

    @Override
    public void queryAll() {
        System.out.println("词典中所有内容如下：");
     //遍历集合
        Iterator iterator = map.keySet().iterator();
       while (iterator.hasNext()){
           String key= (String) iterator.next();
           MyDctionary value = map.get(key);
           System.out.println(value);
       }
    }

    @Override
    public void delete(String name) {
//要先判断 是否存在，存在则删除，否则提示不存在
        if(map.containsKey(name)){
            map.remove(name);
            System.out.println("该单词已经删除");
        }else{
            System.out.println("该单词不存在");
        }
    }
}
